package com.yiming.server01.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yiming.server01.config.RestTemplateconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.sound.midi.Soundbank;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 曾衣铭
 * @Date: 2020/09/26/16:23
 * @Description:
 */
@RestController
public class Server01Controller {
    @Autowired
    private RestTemplate restTemplate;
    //添加服务熔断机制
    @HystrixCommand(fallbackMethod = "fallbackgetsum")
    @GetMapping("/server01_add")
    public String getsum(int x ,int y){
        //rest调用服务server01 返回结果
        System.out.println("x="+x+",y="+y);
        String url = "http://server02/add?x=%d&y=%d";
        String newUrl = String.format(url,x,y);
        String result= restTemplate.getForObject(newUrl,String.class);
        return result;
    }
    public String fallbackgetsum(int x ,int y){
        return "服务正在维护。。。";
    }
}
